<?php
/**
 * LoginForm model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * LoginForm class.
 * LoginForm class is a Form class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	/**
	 * The username field in the form
	 * @var string
	 */
	public $username;
	
	/**
	 * The password field in the form
	 * @var String
	 */
	public $password;
	
	/**
	 * The "Remember Me" checkbox value in the form
	 * @var int
	 */
	public $rememberMe;

	/**
	 * An object of class UserIdentity.
	 * @var UserIdentity
	 */
	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 * @return array the validation rules.
	 */
	public function rules()
	{
		return array(
		// username and password are required
		array('username, password', 'required'),
		// rememberMe needs to be a boolean
		array('rememberMe', 'boolean'),
		// password needs to be authenticated
		array('password', 'authenticate'),
		);
	}

	/**
	 * Returns the labels for each column variables. 
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
		// label of username and password will be automatically generated according to the attribute name.
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
	 * Authenticates the identity of the user.
	 * This is the 'authenticate' validator as declared in rules().
	 * This function will validate the identity of the user and add corresponding 
	 * error in error list of the object. 
	 */
	public function authenticate($attribute,$params)
	{
		// If there is any other validation error, this validation will not be carried out.
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate()){
				// Add validation errors to the error array.
				switch($this->_identity->errorCode){
					case UserIdentity::ERROR_PASSWORD_INVALID:
					case UserIdentity::ERROR_UNKNOWN_IDENTITY:
					case UserIdentity::ERROR_USERNAME_INVALID:
						$this->addError('password','Incorrect username or password.');
						break;
					case UserIdentity::ERROR_USER_HAS_NO_ACCESS:
						$this->addError('username', 'You do not have any access right to the system! You will be able to access the system if the adminstrator grant you access to any organizations in the system.');
						break;
				}
			}
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * This function uses UserIdentity class to authenticate user.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		// if _identity is null, it should be set based on the username and password.
		$this->username=$this->username;
		$this->password=$this->password;
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}

		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			// Registers the identity of the web user.
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
		return false;
	}
}
